package com.krlin.word.word.init.service;

import com.krlin.word.word.init.RedisCacheService;
import com.krlin.word.word.pojo.WordEntity;
import com.krlin.word.word.service.WordMongoService;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author krlin
 */
@Service
public class RedisCacheServiceImpl implements RedisCacheService {

    @Resource
    RedisTemplate<String, Object> redisTemplate;

    @Resource
    MongoTemplate mongoTemplate;

    @Resource
    WordMongoService wordService;

    @Override
    public void cacheAllWords() {
        List<WordEntity> wordEntityList = mongoTemplate.findAll(WordEntity.class);
        wordEntityList.forEach(x -> redisTemplate.opsForSet().add("wordEntitySet", x));
        redisTemplate.opsForValue().set("wordEntityJson", wordEntityList);
    }

    @Override
    public void cacheOneTypeWord(String type) {
        List<WordEntity> wordEntityList = wordService.findWordsByType(type);
        wordEntityList.forEach(x -> redisTemplate.opsForSet().add("wordEntitySetOf"+type, x));
        redisTemplate.opsForValue().set("wordEntityJsonOf"+type, wordEntityList);
    }
}
